﻿using JBotAPI.Logs;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace JBot {
	/// <summary>
	/// Interaction logic for XMLLogsControl.xaml
	/// </summary>
	public partial class XMLLogsControl : UserControl {
		public XMLLogsControl( ) {
			this.InitializeComponent( );
			App.xmpp.Logs.LogAdded += new LogCollection.NewLogEvent(XMLloging);
		}

		public void XMLloging(object sender, NewLogEventArgs e) {
			if (!this.Dispatcher.CheckAccess( )) {
				this.Dispatcher.Invoke(new LogCollection.NewLogEvent(XMLloging), DispatcherPriority.Normal, new object[ ] { sender, e });
			} else {
				if (e._Flag == LogFlags.XMPPXML) {
					string prefix = String.Format("[{0}] - ", DateTime.Now.ToLongTimeString( ));
					Paragraph para = new Paragraph(new Run(prefix + e._Text));
					if (e._Text.IndexOf("Sended:") >= 0) {
						para.Foreground = Brushes.LightSkyBlue;
					} else if (e._Text.IndexOf("Received:") >= 0) {
						para.Foreground = Brushes.Orange;
					}
					this.XLC_RTB.Document.Blocks.Add(para);
					this.XLC_RTB.ScrollToEnd( );
				}
			}
		}

		/*#region Zdarzenia okna
		private void CMS_Opening(object sender, CancelEventArgs e) {
			ContextMenu CMS_Tekst_V = (ContextMenu)sender;
			RichTextBox CMS_Tekst_P = (RichTextBox)CMS_Tekst_V.PlacementTarget;
			CMS_Tekst_V.Items[0].Visibility = !CMS_Tekst_P.IsReadOnly;
			CMS_Tekst_V.Items[1].Visibility = !CMS_Tekst_P.IsReadOnly;
			CMS_Tekst_V.Items[2].Visibility = !CMS_Tekst_P.IsReadOnly;
			CMS_Tekst_V.Items[5].Visibility = !CMS_Tekst_P.IsReadOnly;
			if (CMS_Tekst_P.Selection.IsEmpty) {
				CMS_Tekst_V.Items[3].Visibility = false;
				CMS_Tekst_V.Items[4].Visibility = false;
				CMS_Tekst_V.Items[6].Visibility = false;
				CMS_Tekst_V.Items[7].Visibility = !CMS_Tekst_P.IsReadOnly;
				CMS_Tekst_V.Items[8].Visibility = true;
				CMS_Tekst_V.Items[9].Visibility = false;
			} else {
				CMS_Tekst_V.Items[3].Visibility = true & !CMS_Tekst_P.IsReadOnly;
				CMS_Tekst_V.Items[4].Visibility = true;
				CMS_Tekst_V.Items[6].Visibility = true & !CMS_Tekst_P.IsReadOnly;
				CMS_Tekst_V.Items[7].Visibility = true;
				if (CMS_Tekst_P.Selection.Text.Length == CMS_Tekst_P.ToString().Length) {
					CMS_Tekst_V.Items[8].Visibility = false;
					CMS_Tekst_V.Items[9].Visibility = true;
				} else {
					CMS_Tekst_V.Items[8].Visibility = true;
					CMS_Tekst_V.Items[9].Visibility = true;
				}
			}
		}
		private void CMS_RTB_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
			ContextMenu CMS_Tekst_V = (ContextMenu)sender;
			RichTextBox CMS_Tekst_P = (RichTextBox)CMS_Tekst_V.SourceControl;
			if (CMS_Tekst_P == null) {
				try {
					RichTextBox activeBox = RTB_Serch(this.Controls) as RichTextBox;
					if (activeBox != null && activeBox.ContainsFocus) {
						CMS_Tekst_P = activeBox;
					}
				} catch { }
			}
			if (CMS_Tekst_P != null) {
				if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 0) {
					RTB_ItemUnDo(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 1) {
					RTB_ItemReDo(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 3) {
					RTB_ItemCut(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 4) {
					RTB_ItemCopy(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 5) {
					RTB_ItemPaste(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 6) {
					RTB_ItemDelete(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 8) {
					RTB_ItemSelectAll(CMS_Tekst_P);
				} else if (CMS_Tekst_V.Items.IndexOf(e.ClickedItem) == 9) {
					RTB_ItemUnSelectAll(CMS_Tekst_P);
				}
			}
		}
		private Control RTB_Serch(ControlCollection ctrl) {
			if (ctrl != null) {
				foreach (Control ctrlin in ctrl) {
					try {
						RichTextBox activeBox3 = ctrlin as RichTextBox;
						if (activeBox3 != null && activeBox3.ContainsFocus) {
							return ctrlin;
						}
					} catch { }
					if (ctrlin.Controls != null) {
						Control where = RTB_Serch(ctrlin.Controls);
						if (where != null) {
							return where;
						}
					}
				}
			}
			return null;
		}
		private void RTB_ItemUnDo(RichTextBox RTB) {
			if (RTB != null)
				RTB.Undo( );
		}
		private void RTB_ItemReDo(RichTextBox RTB) {
			if (RTB != null)
				RTB.Redo( );
		}
		private void RTB_ItemCut(RichTextBox RTB) {
			if (RTB != null)
				RTB.Cut( );
		}
		private void RTB_ItemCopy(RichTextBox RTB) {
			if (RTB != null)
				RTB.Copy( );
		}
		private void RTB_ItemPaste(RichTextBox RTB) {
			if (RTB != null)
				RTB.Paste( );
		}
		private void RTB_ItemDelete(RichTextBox RTB) {
			if (RTB != null) {
				string temp = Clipboard.GetText( );
				RTB.Cut( );
				Clipboard.SetText(temp);
			}
		}
		private void RTB_ItemSelectAll(RichTextBox RTB) {
			if (RTB != null)
				RTB.SelectAll( );
		}
		private void RTB_ItemUnSelectAll(RichTextBox RTB) {
			if (RTB != null)
				RTB.Selection.Select(RTB.CaretPosition, RTB.CaretPosition);
		}
		#endregion*/
	}
}